package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.discovery.activity.UserHomePageActivity;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.userInfo.adapter.GiveLikeAdapter;
import com.rongshu.view.userInfo.entity.GiveLikeMessage;
import com.rongshu.view.userInfo.iview.GiveLikeView;
import com.rongshu.view.userInfo.presenter.GiveLikePresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class PraiseMeListActivity extends MessageListActivity<GiveLikePresenter> implements GiveLikeView {
    //adapter
    private GiveLikeAdapter mGiveLikeAdapter = null;
    //map
    private Map<String, Object> map = null;
    //intent
    private Intent intent = null;
    //点赞列表
    private List<GiveLikeMessage.ResponseBean> praiseList;

    @Override
    protected void initView() {
        headMiddle.setText("赞");
        txt_right.setVisibility(View.GONE);
        //presenter
        setPresenter(new GiveLikePresenter(this), this);
        //layoutmanager
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        //adapter
        praiseList = new ArrayList<>();
        mGiveLikeAdapter = new GiveLikeAdapter(this, praiseList);
        mGiveLikeAdapter.setHasStableIds(true);
        mGiveLikeAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(mGiveLikeAdapter);
    }

    @Override
    protected void requestMessage() {
        //user_id
        int user_id = getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        presenter.getLikeMsg(user_id,
                pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }

    @Override
    protected void setAllRead() {

    }

    @Override
    public void onGiveLikeMsg(GiveLikeMessage message) {
        if (message != null) {
            List<GiveLikeMessage.ResponseBean> authorDetailBeans = message.getResponse();
            if (refreshLayout.isRefreshing()) {//结束刷新
                refreshLayout.finishRefresh();
            }
            if (refreshLayout.isLoading()) {//结束加载
                refreshLayout.finishLoadmore();
            }
            if (pageIndex == 1) {
                praiseList.clear();
            }
            praiseList.addAll(authorDetailBeans);
            mGiveLikeAdapter.notifyDataSetChanged();
            if (authorDetailBeans.size() == 0) {//请求不到数据，证明没有更多了
                refreshLayout.finishLoadmoreWithNoMoreData();
            }
            if (praiseList.size() == 0) {//判断显示缺省页面
                showEmptyLayout();
            } else {
                hideEmptyLayout();
            }
        }
    }

    @Override
    public void onItemClick(int position) {
        if (position >= praiseList.size()) return;
        setCurrentMsgRead();
        GiveLikeMessage.ResponseBean bean = praiseList.get(position);
        //跳转到文章页面
        ArticleDetaisRequest request = new ArticleDetaisRequest();
        request.setContribute_id(bean.getContribute_id());
        request.setType(5);
        request.setTopic_id(0);
        Intent intent = new Intent(this, SingleArticleDetailsActivity.class);
        intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, request);
        startActivity(intent);
    }

    /**
     * 点击当前消息设置已阅读
     */
    private void setCurrentMsgRead() {

    }

    @Override
    public void onItemDeleteClick(int position) {

    }

    /**
     * 头像点击
     */
    @Override
    public void onUserHeaderClick(int userIdTo) {
        int user_id = getUserId();
        if (user_id == userIdTo) {
            intent = new Intent(this, PersonDetailsActivity.class);
            startActivity(intent);
        } else {
            intent = new Intent(this, UserHomePageActivity.class);
            intent.putExtra(Constant.USER_ID, userIdTo);
            startActivity(intent);
        }
    }
}
